var stateController = new function(){
	
	var   _$actionState //selector de estado en el formulario de nueva acción
		, _$initState   //selector de estado inicial del objeto
		, _$newStateBtn
		, _$acceptBtn
		, _$cancelBtn
		, _$selectLayer
		, _$inputLayer
	;
	
	var _initDataSource = [
        {
        	text: 'Default',
        	value: 'Default'	
        }
	];
	
	var _dataSource = new kendo.data.DataSource({
	     data: _initDataSource
	});
	
	function empty(){
		
		for (var i = _dataSource.total(); i>1; i--){
			_dataSource.remove(_dataSource.data()[i-1]);
		}
		
		/*
		_dataSource.data({}); 
		*/
		
		_$initState.data("kendoDropDownList").refresh();
		_$actionState.data("kendoDropDownList").refresh();
	}
	
	function init(){
		
		_$initState = $('#initial-state');
		_$actionState = $('#action-state');
		_$input = $('#new-state-input');
		_$newStateBtn = $('#add-state-btn');
		_$selectLayer = $('#state-select-layer');
		_$inputLayer = $('#state-input-layer');
		_$acceptBtn = $('#new-state-accept-btn');
		_$cancelBtn = $('#new-state-cancel-btn');
		
		_$initState
			.kendoDropDownList({
				dataTextField: "text",
	            dataValueField: "value",
	            dataSource: _dataSource
			})
			.data("kendoDropDownList");
		
		_$actionState
			.kendoDropDownList({
				dataTextField: "text",
	            dataValueField: "value",
	            dataSource: _dataSource
			})
			.data("kendoDropDownList");
			
		
		toggleInitSelector(false);
		
		_$newStateBtn.click(function(){
			toggle();
		})
		
		_$acceptBtn.click(function(){
			addNewState();
		})
		
		_$cancelBtn.click(function(){
			toggle();
		})
		
	}//end init function
	
	function toggle(){
		
		_$input.val('');
		_$selectLayer.toggle();
		_$inputLayer.toggle()
	}
	
	function addNewState(){
		
		var newValue = $.trim( _$input.val() );
		
		if (newValue != '' && newValue != 'Default'){
			
			_dataSource.add({text:newValue, value:newValue});
			_$initState.data("kendoDropDownList").refresh();
			_$actionState.data("kendoDropDownList").refresh();
			
			toggle();
		}
	}
	
	function reset(){
		
		_$selectLayer.toggle(true);
		_$inputLayer.toggle(false)
	}
	
	//get del combo de nueva acción
	function getValue(){

		return _$actionState.data("kendoDropDownList").value();
	}
	
	function toggleInitSelector(state){
		
		_$initState.data("kendoDropDownList").select(0)
		_$initState.data("kendoDropDownList").enable(state);
	}
	
	function onChange(){
		
		_$initState.data("kendoDropDownList").bind("change", function(e) {
			
			if(typeof mainCanvas.canvas.getActiveObject() != "undefined"){
    			leftTree.treeView.dataSource.getByUid(mainCanvas.canvas.getActiveObject().node[0].dataset.uid).initState = this.value();
    		}
		});
	}
	
	//set del estado inicial
	function setValue(p_value){
		
		_$initState.data("kendoDropDownList").value(p_value);
		
		return _$initState.data("kendoDropDownList");
	}
	
	function loadValue(p_value){
		
		var newValue = $.trim( p_value );
		
		if (newValue != '' && newValue != 'Default'){
			
			_dataSource.add({text:newValue, value:newValue});
			_$initState.data("kendoDropDownList").refresh();
			_$actionState.data("kendoDropDownList").refresh();
			
			_$initState.data("kendoDropDownList").select(function(dataItem) {
			    return dataItem.text === newValue;
			});
		}
	}
	
	function enable(p_enable){
		
		_$actionState.data("kendoDropDownList").enable(p_enable)
		
		return _$initState.data("kendoDropDownList");
	}
	
	function _getStates(){
		
		var r = [];
		
		$.each(_dataSource.data(), function(i,e){
			
			var object = {
				id: e.uid,
				name: e.text
			}
			
			r.push(object)
		})
		
		return r;
	}
	
	
	this.init = init;
	this.reset = reset;
	this.getValue = getValue;
	this.toggleInitSelector = toggleInitSelector;
	this.onChange = onChange;
	this.setValue = setValue;
	this.loadValue = loadValue;
	this.enable = enable;
	this.getStates = _getStates;
	this.empty = empty;
}